<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="contractForm" :label-width="formLabelWidth" ref="refContractForm" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="应聘者姓名" prop="candidateName">
              <el-input v-model="contractForm.candidateName" placeholder="请选择应聘者姓名" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findCandidate"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同签订日期" prop="contractSignDate">
              <el-date-picker
                  v-model="contractForm.contractSignDate" type="date" placeholder="合同签订日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="合同类型" prop="contractType">
              <el-select v-model="contractForm.contractType" placeholder="请选择合同类型">
                <el-option label="全职" :value="10"></el-option>
                <el-option label="兼职" :value="20"></el-option>
                <el-option label="实习" :value="30"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="生效日期" prop="effectiveDate">
              <el-date-picker
                  v-model="contractForm.effectiveDate" type="date" placeholder="生效日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="到期日期" prop="expiryDate">
              <el-date-picker
                  v-model="contractForm.expiryDate" type="date" placeholder="到期日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="合同状态" prop="state">
              <el-select v-model="contractForm.state" placeholder="请选择合同状态">
                <el-option label="有效" :value="10"></el-option>
                <el-option label="终止" :value="20"></el-option>
                <el-option label="续签" :value="30"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="工作地点" prop="workLocation">
              <el-input v-model="contractForm.workLocation" placeholder="请输入工作地点" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="工作时间" prop="workingHours">
              <el-input v-model="contractForm.workingHours" placeholder="请输入工作时间" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="22">
            <el-form-item label="岗位职责" prop="jobDescription">
              <el-input type="textarea" :rows="3" v-model="contractForm.jobDescription"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="candidateSelectorVisible" v-if="candidateSelectorVisible" title="应聘者基本信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <candidate-selector @candidate-selected-event="handleCandidateSelectedEvent">
      </candidate-selector>
    </el-dialog>
  </div>
</template>

<script>
import contractApi from '@/api/contractApi'
import CandidateSelector from "@/views/candidate/CandidateSelector.vue";
export default {
  name: 'ContractAdd',
  components: {CandidateSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      contractForm: {
        candidateId: '',
        candidateName: '',
        contractSignDate: '',
        contractType: '',
        effectiveDate: '',
        expiryDate: '',
        state: '',
        workLocation: '',
        workingHours: '',
        jobDescription: ''
      },
      rules: {
        candidateName: [
          { required: true, message: '请输入应聘者姓名', trigger: 'blur' }
        ],
        contractSignDate: [
          { required: true, message: '请输入合同签订日期', trigger: 'blur' }
        ],
        contractType: [
          { required: true, message: '请输入合同类型', trigger: 'blur' }
        ],
        state: [
          { required: true, message: '请输入合同状态', trigger: 'blur' }
        ],
        jobDescription: [
          { required: true, message: '请输入岗位职责', trigger: 'blur' }
        ],
      },
      candidateSelectorVisible: false,
    }
  },
  created () {
    if (this.id) {
      contractApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.contractForm = resp.data
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.contractForm = {
        candidateId: '',
        candidateName: '',
        contractSignDate: '',
        contractType: '',
        effectiveDate: '',
        expiryDate: '',
        state: '',
        workLocation: '',
        workingHours: '',
        jobDescription: ''
      }
      this.$emit('reset-contract-add-event')
    },
    onSave () {
      this.$refs.refContractForm.validate((valid) => {
        if (valid) {
          contractApi.save(this.contractForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-contract-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findCandidate () {
      this.candidateSelectorVisible = true
    },
    handleCandidateSelectedEvent (selectedCandidate) {
      this.candidateSelectorVisible = false
      if (selectedCandidate && 'candidateId' in selectedCandidate && this.contractForm.candidateId !== selectedCandidate['candidateId']) {
        this.contractForm.candidateId = selectedCandidate['candidateId']
        this.contractForm.candidateName = selectedCandidate['candidateName']
      }
    },
    onBack () {
      this.$emit('close-contract-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>